#ifndef TREEMODEL_H
#define TREEMODEL_H

#include <QAbstractItemModel>
#include <QVariantMap>
#include <QVector>

struct Node {
    QVariantMap data;
    QVector<Node> children;  // 改为存储Node对象而非QVariantMap
    Node(){}
    Node(const QVariantMap& d) : data(d) {}

};

class TreeModel : public QAbstractItemModel
{
    Q_OBJECT
public:
    explicit TreeModel(const QStringList& headers, QObject *parent = nullptr);
    void setTreeData(const Node& rootData);

    Node treeData() const { return m_rootNode; }

    /* QAbstractItemModel 接口 */
    QModelIndex index(int row, int column,
                      const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QVariant headerData(int section, Qt::Orientation, int role) const override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;

    /* 拖放 */
    QStringList mimeTypes() const override;
    QMimeData* mimeData(const QModelIndexList &indexes) const override;
    bool dropMimeData(const QMimeData *data, Qt::DropAction action,
                      int row, int column, const QModelIndex &parent) override;
    Qt::DropActions supportedDropActions() const override;


    typedef std::function<int()> CallbackFunc;

    void setCallback(CallbackFunc callback) { m_callback = callback; };

    bool isChanged() const { return m_isChanged; }

private:
    Node m_rootNode;
    QStringList m_headers;
    CallbackFunc m_callback; ///< 数据回调函数 获取当前选取的数据列表
    bool m_isChanged = false;
};

#endif // TREEMODEL_H
